﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SignalAccuracyRoundControl.xaml.cs" company="José Miguel Torres">
//   desarrolloMobile.NET 2012
// </copyright>
// <summary>
//   Defines the SignalAccuracyRoundControl type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace devMobile.Controls.Status
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    using Microsoft.Expression.Shapes;

    public partial class SignalAccuracyRoundControl : UserControl, ISignalAccuracyControl, IRangeBehaviour, INotifyPropertyChanged
    {

        private const int _totalSignalas = 8;

        private int _maxValue = 100;

        private int _value;

        private List<RangeBehaviourItem> _rangeBehaviourCollection = new List<RangeBehaviourItem>();

        public SignalAccuracyRoundControl()
        {
            InitializeComponent();
        }

        private void SetVisibility(int rectangle, bool visible)
        {
            if (visible) ((Arc)this.LayoutRoot.FindName(string.Format("s{0}", rectangle))).Visibility = Visibility.Visible;
            else ((Arc)this.LayoutRoot.FindName(string.Format("s{0}", rectangle))).Visibility = Visibility.Collapsed;
        }

        private void SetFillBrush(Brush brush)
        {
            for (int i = 0; i < _totalSignalas; i++)
            {
                ((Arc)this.LayoutRoot.FindName(string.Format("s{0}", i))).Fill = brush;
            }
        }

        public void Draw()
        {
            var _relativeHeight = (this.Value * _totalSignalas) / MaxValue;
            var aux = default(int);
            while (aux < _relativeHeight)
            {
                SetVisibility(aux++, true);
            }
            while (_relativeHeight < _totalSignalas)
            {
                SetVisibility(_relativeHeight++, false);
            }
        }

        public Brush SignalColor
        {
            set
            {
                this.SetFillBrush(value);
            }
        }

        public int MaxValue
        {
            get
            {
                return _maxValue;
            }
            set
            {
                _maxValue = value;
            }
        }

        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                NotifyPropertyChanged("Value");
                Dispatcher.BeginInvoke(this.Draw);
                ApplyBehaviour();
            }
        }

        public void ApplyBehaviour(bool animationWhenNoSignal = true)
        {
            this.ApplyBehaviour(animationWhenNoSignal, _rangeBehaviourCollection);
        }

        public void ApplyBehaviour(bool animationWhenNoSignal,List<RangeBehaviourItem> rangeBehaviours)
        {
            RangeBehaviourCollection = rangeBehaviours;
            foreach (var rangeBehaviour in _rangeBehaviourCollection)
            {
                if ((this.Value >= rangeBehaviour.FromValue) && (this.Value <= rangeBehaviour.ToValue))
                {
                    this.SignalColor = rangeBehaviour.SignalColor;
                    if (animationWhenNoSignal && (this.Value <=  MaxValue / _totalSignalas ))
                    {
                        if (MinAnimation.GetCurrentState() == ClockState.Stopped)
                        {
                            MinAnimation.Begin();
                            MinAnimation.RepeatBehavior = new RepeatBehavior(TimeSpan.FromSeconds(10.0));
                        }
                        this.SetVisibility(0, true);
                    }
                    else
                    {
                        MinAnimation.Stop();
                    }

                }
            }
        }

        public IEnumerable<RangeBehaviourItem> RangeBehaviourCollection
        {
            get
            {
                return _rangeBehaviourCollection;
            }
            set
            {
                _rangeBehaviourCollection = (List<RangeBehaviourItem>)value;
                NotifyPropertyChanged("RangeBehaviourCollection");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
